#!/bin/bash
# author tyb


# 执行结果判断函数
function isgreen() {
    if [ $? -eq 0 ]; then
        echo -e "\e[32m$1==========>succeed\e[0m"
    else
        echo -e "\e[31m$1==========>failed\e[0m"
        # exit 1
    fi
}

# 网络测试函数
function network() {
    #超时时间
    local timeout=1

    #目标网站
    local target=www.baidu.com

    #获取响应状态码
    local ret_code
    ret_code=$(curl -I -s --connect-timeout ${timeout} ${target} -w "%{http_code}" | tail -n1)

    if [ "$ret_code" = "200" ]; then
        #网络畅通
        echo -e "\e[32m网络畅通==========>succeed\e[0m"
        return 0
    else
        #网络不畅通
        echo -e "\e[31m网络不畅通，请检查网络==========>succeed\e[0m"
        return 1
    fi

}

# 帮助命令函数
function print_help() {
  echo "Usage: $0 [OPTIONS]"
  echo "Options:"
  echo "  --help            Display this help message."
  echo "  --hostname,-h     Specify host name. Required"
  echo "  --password,-p     Specify root password. default: Iamadm1n!!"
  echo "  --network-address,-N  Specify time synchronization network address"

  echo "eg: "
  echo "    ./init.sh -h dom0 -N 10.0.0.0/24"

  
  
}


# 收集变量
while [[ $# -gt 0 ]]; do
  case "$1" in
    --help)
       print_help
        exit 0
        ;;
    --hostname|-h)
      if [[ -n "$2" ]]; then
        name="$2"
        shift 2
      else
        echo "Missing argument for --help option."
        exit 1
      fi
      ;;
    --password|-p)
      if [[ -n "$2" ]]; then
        passwd="$2"
        shift 2
      else
        echo "Missing argument for --help option."
        exit 1
      fi
      ;;
    --network-address|-N)
      if [[ -n "$2" ]]; then
        network_address="$2"
        shift 2
      else
        echo "Missing argument for --help option."
        exit 1
      fi
      ;;
    *)
      echo "Invalid option: $1"
      exit 1
      ;;
  esac
done

# 过滤变量
hostname=$name
if [[ -z "$hostname" ]]; then
    echo "--hostname options are required."
    exit 1
fi
password=$passwd
time_network_address=$network_address
if [[ -z "$time_network_address" ]]; then
    echo "--network-address options are required."
    exit 1
fi



# 初始化root密码
echo "初始化root密码"
echo "root:${password:-"Iamadm1n!!"}" | sudo chpasswd
isgreen "初始化root密码"

# 修改主机名
echo "修改主机名"
sudo hostnamectl set-hostname "$hostname"
isgreen "修改主机名为：$hostname"

#优化ssh连接
echo "优化ssh连接"
sudo sed -i 's/^#PermitRootLogin.*/PermitRootLogin yes/g' /etc/ssh/sshd_config
sudo sed -i 's/#GSSAPIAuthentication.*/GSSAPIAuthentication no/g' /etc/ssh/sshd_config
sudo sed -i 's/#UseDNS.*/UseDNS no/g' /etc/ssh/sshd_config
isgreen "修改配置文件"
sudo systemctl restart ssh
isgreen "重启ssh服务"

# 关闭交换分区
echo "关闭交换分区"
swapoff -a
isgreen "临时关闭交换分区"
sed -i '/swap/s/^/#/' /etc/fstab
isgreen "永久关闭交换分区"

# 安装时间同步服务
echo "更新apt源"
network
code=$?
if [ $code -eq 0 ]; then
    sudo apt-get -y update >/dev/null 2>&1
    isgreen "更新apt源"

    echo "安装chrony"
    sudo apt-get install -y chrony  >/dev/null 2>&1
    isgreen "安装chrony"
fi
# 修改配置文件
if [ ! -f "/etc/chrony/chrony.conf.bak" ]; then
    cp "/etc/chrony/chrony.conf" "/etc/chrony/chrony.conf.bak"  >/dev/null 2>&1
    isgreen "备份配置文件"
fi
# 添加时间服务器
if ! grep -q "$network_address" "/etc/chrony/chrony.conf"; then
    # 清除默认的时间服务器配置
    sed -i '/^pool/d' "/etc/chrony/chrony.conf"  >/dev/null 2>&1
    isgreen "清除默认的时间服务器配置"
    sed -i '2a\server 10.177.1.172 iburst' "/etc/chrony/chrony.conf"  >/dev/null 2>&1
    isgreen "配置时间服务器：10.177.1.172"
    sed -i '2a\server 10.177.40.177 iburst' "/etc/chrony/chrony.conf"  >/dev/null 2>&1
    isgreen "配置时间服务器：10.177.40.177"
    cat << EOF >> /etc/chrony/chrony.conf
makestep 1 3
allow $network_address
local stratum 10
maxdistance 16.0
EOF
isgreen "允许 $network_address 与我同步时间"

fi
# 启动服务
systemctl restart chronyd >/dev/null 2>&1
isgreen "重启chronyd服务"

# 关闭自动更新
echo "关闭自动更新"
sudo sed -i 's/1/0/g' /etc/apt/apt.conf.d/10periodic
sudo sed -i 's/1/0/g' /etc/apt/apt.conf.d/20auto-upgrades
grep 1 /etc/apt/apt.conf.d/20auto-upgrades /etc/apt/apt.conf.d/10periodic
code=$?
if [ ! $code -eq 0 ]; then
    echo "123" >/dev/null 2>&1 
    isgreen "关闭自动更新"
fi

echo "关闭fwupd"
sudo systemctl disable fwupd.service --now >/dev/null 2>&1
sudo systemctl mask fwupd.service >/dev/null 2>&1
isgreen "关闭fwupd"

echo "关闭snapd"
sudo systemctl disable snapd --now >/dev/null 2>&1
sudo systemctl mask snapd >/dev/null 2>&1
isgreen "关闭snapd"

echo "修改系统提示重启服务的形式"
sed -i "s/#\$nrconf{restart}.*/\$nrconf{restart} = 'l';/g" /etc/needrestart/needrestart.conf
isgreen "修改系统提示重启服务的形式"

echo "安装软件 nfs-server ceph-common  keepalived"
apt install -y nfs-server ceph-common  keepalived >/dev/null 2>&1
isgreen "安装软件 nfs-server ceph-common  keepalived"